<?php

namespace App\Dao;

use App\Model\OrderLineModel;
use App\Model\OrdersModel;

class OrderLineDao
{
    /**
     * @param $params
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public static function builder_query($params = []): \Illuminate\Database\Eloquent\Builder
    {
        $query = OrderLineModel::query();
        $query->when(checkIsHasField($params, 'order_id'), function ($query) use ($params) {
            $query->where('order_id', $params['order_id']);
        });
        $query->when(checkIsHasField($params, 'line_id'), function ($query) use ($params) {
            $query->where('line_id', $params['line_id']);
        });
        $query->when(checkIsHasField($params, 'user_id'), function ($query) use ($params) {
            $query->where('user_id', $params['user_id']);
        });
        return $query;
    }

    /**
     * @param $params
     * @return \Illuminate\Database\Eloquent\Collection
     */
    public static function get_orders_line($params = []): \Illuminate\Database\Eloquent\Collection{
        return self::builder_query($params)->with(['userInfo'])->get();
    }
}
